import { connect } from 'dva';
import * as React from 'react';

const Permission: React.FC<any> = (props: any) => {
  const checkPermission = () => {
    if (props.permission.permissionList && props.permission.permissionList.length > 0) {
      return !!props.permission.permissionList.find((item: any) => item === props.permsCode);
    }
    return false;
  };

  return checkPermission() ? props.children : '';
};

export default connect(({ permission }: any) => ({ permission }))(Permission);

export const checkPermission = (permissions: any, permsCode: string) => {
  if (permissions.permissionList && permissions.permissionList.length > 0) {
    return !!permissions.permissionList.find((item: any) => item === permsCode);
  }
  return false;
};
